package util;

import java.nio.ByteBuffer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class StringUtil {
	
	private static char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
		'A', 'B', 'C', 'D', 'E', 'F' };	
	
	public static String[] split(String src ,String p)
	{
		List<String> list = new ArrayList<String> ();
		int curind = 0;
		for( int ind=-1; (ind= src.indexOf(p,curind)) >=0 ;)
		{
			list.add( src.substring(curind,ind));
			curind = ind+p.length();
		}
		
		list.add( src.substring(curind,src.length()));
			
		return list.toArray(new String[list.size()]);
	}
    public static String formatHHmm( Date date){
        return new SimpleDateFormat("HH:mm").format(date);
    }
    public static String formatHHmm( String  date){
        return new SimpleDateFormat("HH:mm").format(date);
    }

    public static Date parseyyyyMMdd( String strdate) throws ParseException{
		return new SimpleDateFormat("yyyyMMdd").parse(strdate);
	}
	public static Date parseBirthday( String strdate) throws ParseException{
		return new SimpleDateFormat("yyyy-MM-dd").parse(strdate);
	}
	public static String formatyyyyMMddHHmmss( Date date){

		return new SimpleDateFormat("yyyyMMddHHmmss").format(date);
	}
	
	public static String formatyyyyMMddHHmmss2( Date date){
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
	}
	
	public static Date parseyyyyMMddHHmmss( String strdate) throws ParseException{
		return new SimpleDateFormat("yyyyMMddHHmmss").parse(strdate);
	}	
	
	public static String replaceAll(String source,String repstr,String newstr){
		String[] arr = split(source,repstr);
		StringBuffer strBuf = new StringBuffer();
		for(int i=0; i< arr.length;i++){
			strBuf.append( arr[i] );
			if(i< arr.length -1)
				strBuf.append(newstr);
		}
		return strBuf.toString();
	}
	
	public static String formatHHmmssSSS(long millsecond){
		 return millsecond /3600000L +":" +   (millsecond /60000)%60
			+":" + 	(millsecond /1000)%60 +"." + millsecond%1000 ;
	}
	
	public static String formatyyyyMMdd(long datetime){
		 return String.valueOf(datetime / 1000000 );
	}
	
	public static String byteArray2FHex(byte[] arrbyte) {
		return byteArray2FHex( arrbyte, 0 , arrbyte.length);
	}

	public static String byteArray2FHex(byte[] arrbyte,int start,int endInd) {
		int eInd = endInd > arrbyte.length ? arrbyte.length : endInd;
		char[] arrayCh = new char[ ( eInd -start) *3];
		for(int i= start,j=0 ; i< eInd ;i++ ,j++ ) {
			arrayCh[3*j]= hexDigit[( arrbyte[i] >> 4) & 0x0f];
			arrayCh[3*j+1] = hexDigit[ arrbyte[i] & 0x0f];
			arrayCh[3*j+2] = ' ';
		}
		return new String(arrayCh);
	}
	
	public static String byteArray2Hex(byte[] arrbyte) {
		return byteArray2Hex( arrbyte, 0 , arrbyte.length);
	}

	public static String byteArray2Hex(byte[] arrbyte,int start,int endInd) {
		int eInd = endInd > arrbyte.length ? arrbyte.length : endInd;
		char[] arrayCh = new char[ ( eInd -start) * 2 ];
		for(int i= start,j=0 ; i< eInd ;i++ ,j++ ) {
			arrayCh[2*j]= hexDigit[( arrbyte[i] >> 4) & 0x0f];
			arrayCh[2*j+1] = hexDigit[ arrbyte[i] & 0x0f];
		}
		return new String(arrayCh);
	}
	
	public static byte[] hex2ByteArray( String hexstr) {
		byte[] buf = new byte[hexstr.length()/2];
		int len = hexstr.length()/2;
		for(int i=0; i<len;i++ ){
			char ch1 = hexstr.charAt(2*i);
			char ch2 = hexstr.charAt(2*i +1);
			int val=0;
			val = hex2Int(ch1,val);
			val = hex2Int(ch2,val);
			buf[i]=(byte)val;
		}
		return buf;
	}
	
	final private static int hex2Int(char ch ,int val){
		
		switch(ch){
		case '0':	case '1': case '2': case '3': case '4':	
		case '5': case '6': case '7': case '8': case '9':
			val = (val <<4) | ( (ch-'0')&0xf);
			break;
		case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
			val = (val <<4) | ( (ch-55)&0xf);
			break;
		case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
			val = (val <<4) | ( (ch-87)&0xf);
			break;
		default:
			throw new  NumberFormatException(""+ch);
		}
		
		return val;
		
	}
	
	
	public static String byteBuffer2FHex(ByteBuffer  arrbyte) {
		return byteBuffer2FHex( arrbyte, 0 , arrbyte.limit());
	}

	public static String byteBuffer2FHex(ByteBuffer arrbyte,int start,int endInd) {
		int eInd = endInd > arrbyte.limit() ? arrbyte.limit() : endInd;
		char[] arrayCh = new char[ ( eInd -start) *3];
		for(int i= start,j=0 ; i< eInd ;i++ ,j++ ) {
			arrayCh[3*j]= hexDigit[( arrbyte.get(i) >> 4) & 0x0f];
			arrayCh[3*j+1] = hexDigit[ arrbyte.get(i) & 0x0f];
			arrayCh[3*j+2] = ' ';
		}
		return new String(arrayCh);
	}
	
	public static long hex2Long(String hex){
		long val =0;
		int len = hex.length();
		if(len > 16)
			throw new  NumberFormatException(hex);
		for(int i=0; i< len;i++){
			char ch = hex.charAt(i);
			switch(ch){
			case '0':	case '1': case '2': case '3': case '4':	
			case '5': case '6': case '7': case '8': case '9':
				val = (val <<4) | ( (ch-'0')&0xf);
				break;
			case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
				val = (val <<4) | ( (ch-55)&0xf);
				break;
			case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
				val = (val <<4) | ( (ch-87)&0xf);
				break;
			default:
				throw new  NumberFormatException(hex);
			}
		}
		return val;
	}
	
	public static String  long2Hex(long lval){
		return String.format("%1$016x", lval);
	}
	
	public static String formatUUID(UUID uuid){
		return String.format("%1$016x", uuid.getMostSignificantBits())+ String.format("%1$016x", uuid.getLeastSignificantBits());
	}
	
	public static String escXMLAttr( String  attrtxt){
		if(attrtxt==null)
			return null;
		StringBuilder strb = new StringBuilder();
		for(int i=0; i< attrtxt.length();i++){
			char ch = attrtxt.charAt(i);
			if(ch == '"'){
				strb.append("&quot;");
			}else if( ch == '\''){
				strb.append("&apos;");
			}else if( ch == '&'){
				strb.append("&amp;");
			}else if( ch == '<'){
				strb.append("&lt;");
			}else if( ch == '>'){
				strb.append("&gt;");
			}else{
				strb.append(ch);
			}
		}
		
		return strb.toString();
	}
	
	
	public static String escXMLText( String  attrtxt){
		if(attrtxt==null)
			return null;
		StringBuilder strb = new StringBuilder();
		for(int i=0; i< attrtxt.length();i++){
			char ch = attrtxt.charAt(i);
			if( ch == '&'){
				strb.append("&amp;");
			}else if( ch == '<'){
				strb.append("&lt;");
			}else if( ch == '>'){
				strb.append("&gt;");
			}else{
				strb.append(ch);
			}
		}
		
		return strb.toString();
	}	
	
	public static String isContainsChar(String param){
		if(param.contains("_")){
			int index = param.indexOf("_");
			String paramId=param.substring(0, index);
			return paramId;
		}
		return param;
	}

	public static String getRequiredString(String parameter) {
		if(parameter==null){
			return "";
		}
		return parameter;
	}

}
